package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.SysMeeting;
import com.ruoyi.system.service.ISysMeetingService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会议Controller
 *
 * @author ruoyi
 * @date 2024-09-23
 */
@RestController
@RequestMapping("/system/meeting")
public class SysMeetingController extends BaseController {
    @Autowired
    private ISysMeetingService sysMeetingService;

    /**
     * 查询会议列表
     */
    @GetMapping("/hisList")
    public TableDataInfo hisList(SysMeeting sysMeeting) {
        startPage();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (user == null) {
            throw new ServiceException("获取用户异常");
        }
        sysMeeting.setUserId(user.getUserId());
        List<SysMeeting> list = sysMeetingService.selectSysMeetingList(sysMeeting);
        return getDataTable(list);
    }

    /**
     * 查询未结束会议
     *
     * @param sysMeeting
     * @return
     */
    @GetMapping("/unClosedList")
    public AjaxResult list(SysMeeting sysMeeting) {
        sysMeeting.setStatus("0");
        List<SysMeeting> list = sysMeetingService.selectSysMeetingList(sysMeeting);
        return success(list);
    }


    /**
     * 获取会议详细信息
     */
    @GetMapping(value = "/{roomId}")
    public AjaxResult getInfo(@PathVariable("roomId") Long roomId) {
        return success(sysMeetingService.selectSysMeetingByRoomId(roomId));
    }

    /**
     * 验证进入会议信息
     */
    @PostMapping(value = "/validate")
    public AjaxResult validate(@RequestBody SysMeeting sysMeeting) {
        SysMeeting meeting = sysMeetingService.selectSysMeetingByRoomId(sysMeeting.getRoomId());
        if (meeting != null && sysMeeting.getPassword() != null
                & sysMeeting.getPassword().equals(meeting.getPassword())) {
            return success("验证通过");
        } else {
            return error("验证失败");
        }
    }

    /**
     * 新增会议
     */
    @PostMapping(value = "/add")
    public AjaxResult add(@Validated @RequestBody SysMeeting sysMeeting) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (user == null) {
            return AjaxResult.error(HttpStatus.NO_CONTENT, "未获取用户信息");
        }
        sysMeeting.setUserId(user.getUserId());
        sysMeeting.setDelFlag("0");
        sysMeeting.setStatus("0");
        sysMeeting.setStartTime(new Date());
        return toAjax(sysMeetingService.insertSysMeeting(sysMeeting));
    }

    /**
     * 结束会议
     */
    @PostMapping(value = "/end")
    public AjaxResult end(@RequestBody SysMeeting sysMeeting) {
        // 非正常结束
        if (!"1".equals(sysMeeting.getStatus())) {
            sysMeeting.setRespectEndTime(DateUtils.addMinutes(new Date(),10));
        }
        return toAjax(sysMeetingService.updateSysMeeting(sysMeeting));
    }

    /**
     * 删除会议
     */
    @DeleteMapping("/{roomIds}")
    public AjaxResult remove(@PathVariable Long[] roomIds) {
        return toAjax(sysMeetingService.deleteSysMeetingByRoomIds(roomIds));
    }
}
